package com.itheima.service.impl;

import com.itheima.constant.MessageConstant;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.Result;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.POIUtils;
import org.apache.dubbo.config.annotation.Service;
import org.checkerframework.checker.units.qual.UnitsMultiple;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
//@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Override
    public Result importOrderSettings(List<String[]> readExcel) {
        //System.out.println("上传的文件是:"+readExcel);
            try {
                //代表插入的总条数
                int count = 0;
                //System.out.println("sheet中的数据有:"+readExcel);
                if (readExcel == null || readExcel.size() == 0) {
                    return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
                } else {
                    //遍历集合,获取一行的的数据
                    for (String[] row : readExcel) {
                        //创建orderSetting对象
                        OrderSetting orderSetting = new OrderSetting();
                        //将一行的数据封装到实体类中
                        orderSetting.setOrderDate(DateUtils.parseString2Date(row[0],
                                "yyyy/MM/dd"));
                        orderSetting.setNumber(Integer.parseInt(row[1]));

                        //判断数据库中是否已经存在该日期对象数据
                        int i = orderSettingMapper.findOrderSettingByOrderDate(orderSetting.getOrderDate());
                        if (i == 0) {
                            //不存在就添加
                            //调用方法,将对象存到数据库zhong
                            orderSettingMapper.importOrderSettings(orderSetting);
                            count++;
                        } else {
                            //存在,就修改原有的预约数量
                            orderSettingMapper.updateOrderSettings(orderSetting);
                            count++;
                        }
                    }
                    //System.out.println("表格条数:" + readExcel.size() + "=====" + count);
                    if (count == readExcel.size()) {
                        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
                    } else {
                        return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
            }
    }

    @Override
    public Result findAllByMoth(String year, String month) {
        //创建起始日期和结束日期
        String begin = year+"-"+month+"-1";
        String end = year+"-"+month+"-31";
        System.out.println(begin);
        System.out.println(end);

        List<OrderSetting> orderSettings = orderSettingMapper.findAllByMoth(begin,end);

        //System.out.println("原始数据为:"+orderSettings);

        if (orderSettings != null) {
            //创建集合存储修饰日期后的OrderSetting
            List<Map> maps = new ArrayList<Map>();

            //遍历OrderSettings
            for (OrderSetting orderSetting : orderSettings) {
                //创建map集合,key=属性名 value=属性值
                Map<String, Object> map = new HashMap<>();
                map.put("date",orderSetting.getOrderDate().getDate());
                map.put("number",orderSetting.getNumber());
                map.put("reservations",orderSetting.getReservations());

                //将map集合存到map集合中
                maps.add(map);
            }

            //System.out.println("修饰后的数据为:"+maps);

            if (maps == null) {
                return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
            }else{
                return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,maps);
            }
        }else{
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    @Override
    public Result updateOrderSettingByDate(String updateDate,String number) {
        int num = 0;
        if (number != null) {
            num = Integer.parseInt(number);
            int row = orderSettingMapper.updateOrderSettingByDate(updateDate,num);
            if (row == 0){
                return new Result(false,MessageConstant.EDIT_ORDERSETTING_FAIL);
            }else{
                return new Result(true,MessageConstant.EDIT_ORDERSETTING_SUCCESS);
            }
        }else{
            return new Result(false,MessageConstant.EDIT_ORDERSETTING_FAIL);
        }
    }
}
